package io.jee.runshan.controller.web;

import io.jee.medusa.orm.condition.Condition;
import io.jee.medusa.orm.condition.ConditionGroup;
import io.jee.medusa.orm.pager.Pager;
import io.jee.medusa.vo.ResultBean;
import io.jee.runshan.domain.Product;
import io.jee.runshan.service.IProductService;
import io.jee.runshan.utils.Utils;

import javax.annotation.Resource;

import org.springframework.beans.BeansException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 前台商品Controller
 * 
 * @author SunX
 *
 */
@Controller
@RequestMapping("/web/product/")
public class WebProductController {

	private static final String LIST = "web/weixin/product/list";
	private static final String DETAIL = "web/weixin/product/detail";

	@Resource
	private IProductService productService;

	@RequestMapping("list")
	public ModelAndView list() {
		return new ModelAndView(LIST);
	}

	@RequestMapping(value = "/ajaxList", produces = { "application/json" })
	@ResponseBody
	public ResultBean<Pager<Product>> ajaxList(
			@RequestParam(value = "pageno", required = true, defaultValue = "1") Integer pageno,
			@RequestParam(value = "productName", required = false) String productName) {
		Pager<Product> pager = null;
		try {
			ConditionGroup group = new ConditionGroup();
			group.and(Condition.onBool("delFlag").eq(false));
			group.and(Condition.onNumber("state").eq(Product.STATE_ON));
			pager = productService.list(new Pager<Product>(pageno, 10), group, "DESC", "publishTime");
		} catch (BeansException e) {
			e.printStackTrace();
			return ResultBean.result(false, "操作异常，请稍候再试！");
		}
		return ResultBean.result(true, "获取订单详情成功", pager);
	}

	@RequestMapping("detail")
	public ModelAndView detail(Product product) {
		if (!Utils.isNullOrEmpty(product.getId())) {
			product = productService.get(product.getId());
		}
		return new ModelAndView(DETAIL).addObject("product", product);
	}
}
